<?php
namespace app\index\model;

class Term extends Base
{
    private $termList = [];
    private $childList = [];
    private $navList = [];

    /**
     * 构造方法
     */
	public function __construct()
	{
		 parent::__construct();
         $this->_initList();
    }

    /**
     * 初始化内建列表
     */
    private function _initList()
    {
        S([
            'type'=>'File', // 缓存类型为File
            'expire'=>3600, // 缓存有效期为永久有效
            'prefix'=>'think'
        ]);
        if(!APP_DEBUG&&
            !empty($this->termList  = S('termList'))&&
            !empty($this->childList = S('childList'))&&
            !empty($this->navList   = S('navList')))
            return true;

        trace('initTermList', 'log');
        $childList = $this->childList;
        $navList = $this->navList;
        $termList = $this->order('listorder asc')->select();
        $termList = $this->forJsonDecode($termList, ['tags_value'=>'tag']);
        $newList = [];
        foreach($termList as $vo){
            $newList[$vo['id']] = $vo;
            $childList[$vo['parent_id']][] = $vo;
            if($vo['display_nav'])
                $navList[$vo['parent_id']][] = $vo;
        }

        if(!APP_DEBUG){
            S('termList',$newList);
            S('childList',$childList);
            S('navList',$navList);
        }

        $this->termList     = $newList;
        $this->childList    = $childList;
        $this->navList      = $navList;
    }

    /**
     * 获取内建栏目列表
     */
    private function _getTermList()
    {
        if(empty($this->termList))
            $this->_initList();
        return $this->termList;
    }

    /**
     * 获取内建子栏目列表
     */
    private function _getChildList()
    {
        if(empty($this->childList))
            $this->_initList();
        return $this->childList;
    }

    /**
     * 获取内建导航列表
     */
    private function _getNavList()
    {
        if(empty($this->navList))
            $this->_initList();
        return $this->navList;
    }

    /**
     * getInfo方法
     * @param $id 要获取的栏目ID
     * @result 格式化后的栏目信息
     */

    public function getInfo($id)
    {
        if(empty($id))
            return false;
        if(isset($this->_getTermList()[$id]))
            return $this->_getTermList()[$id];
        $result = $this->find($id);
        $result = $this->forJsonDecode($result, ['tags_value'=>'tag']);
        return $result;
    }

    /**
     * getAddress方法
     * @param $id 要获取的栏目ID
     * @result array 栏目的路径数组
     */

    public function getAddress($id)
    {
        if(empty($id))
            return false;
        $address = [];
        while($id!=0){
            $info = $this->getInfo($id);
            $address[$id]=$info; 
            $id = $info['parent_id'];
        }
        $address = array_reverse($address, true);
        return $address;
    }

    /**
     * 获取栏目的父栏目id
     * @param $id 要获取的栏目ID
     * @result int 栏目的父栏目ID
     */
    public function getParentId($id)
    {
        if(empty($id))
            return false;
        $result = $this->getInfo($id);
        return $result['parent_id'];
    }

    /**
     * 获取栏目的model_id
     * @param $id 要获取的栏目ID
     * @result int 栏目的model_ID
     */
    public function getModelId($id)
    {
        if(empty($id))
            return false;
        $result = $this->getInfo($id);
        return $result['model_id'];
    }

    /**
     * getIdByFlag方法
     * @param $flag 要获取ID的栏目Flag
     * @result 对应栏目的ID
     */

    public function getIdByFlag($flag)
    {
        if(empty($flag))
            return false;
        $where['flag'] = $flag;
        $result = $this->field('id')->where($where)->find();
        return $result['id'];
    }

    //获取某栏目的子栏目
    public function getTermChild($parentId, $param=null)
    {
        if(isset($param['where'])&& isset($param['where']['display_nav']))
            $childList = $this->_getNavList();
        else
            $childList = $this->_getChildList();
        if(!empty($childList))
            if(!isset($childList[$parentId]))
                return [];
            else
                return $childList[$parentId];
        $where['parent_id']=$parentId;
        $data = $this->where($where)->order('listorder asc')->select();
        return $data;
    }

    //获取栏目的所有子孙栏目的ID
    public function getAllChildId($topId = 0)
    {
        $newTermArr = [];
        $childArr = $this->_getChildList();
        if(!isset($childArr[$topId]))
            return [];
        $funGet = function ($id) use (&$newTermArr, $childArr, &$funGet) {
            foreach($childArr[$id] as $termInfo){
                $newTermArr[] = $termInfo['id'];
                if(isset($childArr[$termInfo['id']]))
                    $funGet($termInfo['id']);
            }
        };
        $funGet($topId);//闭包引用传入$newTermArr
        return $newTermArr;
    }
}

